//
//  SZLNavigationController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/18.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLNavigationController.h"

@interface UINavigationController (UINavigationControllerNeedShouldPopItem)

- (BOOL)navigationBar:(UINavigationBar*)navigationBar shouldPopItem:(UINavigationItem*)item;

@end
@interface SZLNavigationController ()<UINavigationBarDelegate,UIGestureRecognizerDelegate>

@end

@implementation SZLNavigationController

//+ (void)initialize
//{
//    UINavigationBar *navBar = [UINavigationBar appearance];
////    navBar.barTintColor = [UIColor whiteColor];
//    navBar.translucent = NO;
//    navBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};
//    
////    [navBar setBackgroundImage:[UIImage imageNamed:@"beijing"] forBarMetrics:UIBarMetricsDefault];
////    下面方法返回按钮会偏移
////    navBar.backIndicatorImage = [UIImage imageNamed:@"btn_back_normal"];
////    navBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"btn_back_normal"];
////    navBar.tintColor = [UIColor whiteColor];
//    
//    UIImage *backImage = [[UIImage imageNamed:@"btn_back_normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
//    UIBarButtonItem *item = [UIBarButtonItem appearance];
//    [item setBackButtonBackgroundImage:backImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//    [item setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //改全屏手势返回
    UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
    gesture.enabled = NO;
    UIView *gestureView = gesture.view;
    
    UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] init];
    popRecognizer.delegate = self;
    popRecognizer.maximumNumberOfTouches = 1;
    [gestureView addGestureRecognizer:popRecognizer];
    
    /**
     *  获取系统手势的target数组
     */
    NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
    /**
     *  获取它的唯一对象，我们知道它是一个叫UIGestureRecognizerTarget的私有类，它有一个属性叫_target
     */
    id gestureRecognizerTarget = [_targets firstObject];
    /**
     *  获取_target:_UINavigationInteractiveTransition，它有一个方法叫handleNavigationTransition:
     */
    id navigationInteractiveTransition = [gestureRecognizerTarget valueForKey:@"_target"];
    /**
     *  通过前面的打印，我们从控制台获取出来它的方法签名。
     */
    SEL handleTransition = NSSelectorFromString(@"handleNavigationTransition:");
    /**
     *  创建一个与系统一模一样的手势，我们只把它的类改为UIPanGestureRecognizer
     */
    [popRecognizer addTarget:navigationInteractiveTransition action:handleTransition];
}
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    /**
     *  这里有3个条件不允许手势执行，1、当前控制器为根控制器；2、如果这个push、pop动画正在执行（私有属性）3,不是有效的向右滑动
     */
    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue] && [self isPullRight:[gestureRecognizer translationInView:self.view]];
}
- (BOOL)isPullRight:(CGPoint)translation
{
    if (translation.x>0) {
        return YES;
    }
    return NO;
}
//pop返回调用  拦截返回点击
- (BOOL)navigationBar:(UINavigationBar*)navigationBar shouldPopItem:(UINavigationItem*)item
{
    UIViewController *vc = self.topViewController;
    if (item != vc.navigationItem) {
        return [super navigationBar:navigationBar shouldPopItem:item];
    }
    if ([vc conformsToProtocol:@protocol(SZLNavigationControllerDelegate)]) {
        if ([(id<SZLNavigationControllerDelegate>)vc szlNavigationControllershouldPopItem:self]) {
            return [super navigationBar:navigationBar shouldPopItem:item];
        }else{
            return NO;
        }
    }else{
        return [super navigationBar:navigationBar shouldPopItem:item];
    }
    return YES;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    if (self.viewControllers.count>0) {
//        自定义系统的侧滑返回会失效
//        UIButton *backBtn = [[UIButton alloc]init];
//        [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_normal"] forState:UIControlStateNormal];
//        [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_pressed"] forState:UIControlStateHighlighted];
//        [backBtn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
//        [backBtn sizeToFit];
//        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
//        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

@end
